package com.example.recharge;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class recharge_record extends AppCompatActivity{
    private SQLiteDatabase db;
    private Context mContext;
    private ListView ListView1;
    private List<recharge_record1> recharge_record1s= new ArrayList<>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recharge_list1);
        //创建一个帮助类对象
        MySqliteOpenHelper mySqliteOpenHelper = new MySqliteOpenHelper(this);
        //调用getReadableDatabase方法,来初始化数据库的创建
        db= mySqliteOpenHelper.getReadableDatabase();
        ListView1=findViewById(R.id.lv1);
        sedRequestWithOkHttp();
        mContext=this;
    }
    //利用网络状态来判断取值是服务器还是数据库
    public boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {

            } else{

            }
        }
        return false;
    }
    //集合排序
    private class sort implements Comparator<recharge_record1> {
        @Override
        public int compare(recharge_record1 t1, recharge_record1 t2) {
            return (int) (t1.getData().get(0).getCharge()-t2.getData().get(0).getCharge());//降序。如果升序位置调换
        }
        List<recharge_record1> fileList=new ArrayList<recharge_record1>();

    }
    private void  sedRequestWithOkHttp(){
        new Thread(new Runnable() {
            private Object recharge_record1;
            @Override
            public void run() {
                try {
                    OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder()
                            .url("http://hh1.me:5001/getchargelog")
                            .build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
                    showResponse(responseData);
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void showResponse(final String response){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Gson gson=new Gson();
                recharge_record1 carlist1=gson.fromJson(response,recharge_record1.class);
                recharge_record1s.add(carlist1);
                //把数据存放到数据库
                final int size = recharge_record1s.get(0).getData().size();
                for (int i=0;i<size;i++){
                    ContentValues values=new ContentValues();
                    //values.put("_id",i+"");
                    values.put("time",recharge_record1s.get(0).getData().get(i).getTime());
                    values.put("licenseplate",recharge_record1s.get(0).getData().get(i).getCarno());
                    values.put("name",recharge_record1s.get(0).getData().get(i).getWho());
                    values.put("balance",recharge_record1s.get(0).getData().get(i).getAmount());
                    values.put("rechargeamount",recharge_record1s.get(0).getData().get(i).getCharge());
                    db.insert("qwe",null,values);
                    //db.delete("info","_id=2","");
                }
                ListView1.setAdapter(new MEAdapter());
            }
        });
    }
    class  MEAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return recharge_record1s.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            Collections.sort(recharge_record1s.get(0).getData(),new sort1());
            Collections.reverse(recharge_record1s.get(0).getData());
            View view1=View.inflate(recharge_record.this,R.layout.recharge_list,null);
            TextView time=view1.findViewById(R.id.tv6);
            TextView VehicleOwner=view1.findViewById(R.id.tv61);
            TextView number=view1.findViewById(R.id.tv62);
            TextView RechargeAmount=view1.findViewById(R.id.tv63);
            TextView SurplusAmount=view1.findViewById(R.id.tv64);
            time.setText("充值时间："+recharge_record1s.get(0).getData().get(i).getTime());
            VehicleOwner.setText("车主："+recharge_record1s.get(0).getData().get(i).getWho());
            number.setText("车牌号："+recharge_record1s.get(0).getData().get(i).getCarno());
            RechargeAmount.setText("充值金额:"+recharge_record1s.get(0).getData().get(i).getCharge());
            SurplusAmount.setText("剩余余额："+recharge_record1s.get(0).getData().get(i).getAmount());
            return view1;
        }
    }
}
